#include "tools.h"
#include "ecfp.h"

/**
 * 测试http://www.pediy.com/kssd/pediy06/pediy6014.htm网页所提及的例子
 */
bool Test_SimpleEC() {

	bool r = true;

	// 曲线为 y^2 = x^3 + x + 1 (mod 23)

	ECFpWork ec;
	
	uint8_t m[] = { 23 }; 
	uint8_t a[] = { 1 }; 
	uint8_t b[] = { 1 }; 
	ECFp_Initialize(&ec, 8, m, a, b);

	//P(3,10)，Q(9,7)
	UInteger i3, i10, i9, i7;
	UInteger_FromUInt(&i3, 3);
	UInteger_FromUInt(&i10, 10);
	UInteger_FromUInt(&i9, 9);
	UInteger_FromUInt(&i7, 7);
	ECPoint p, q;
	ECPoint_FromUInt(&p, &i3, &i10, 0);
	ECPoint_FromUInt(&q, &i9, &i7, 0);

	{
		ECPoint pq;
		ECFp_Add(&ec, &p, &q, &pq);

		ECPoint pqt;
		UInteger i17, i20;

		UInteger_FromUInt(&i17, 17);
		UInteger_FromUInt(&i20, 20);
		ECPoint_FromUInt(&pqt, &i17, &i20, 0);

		r = ECPoint_Equal(&pq, &pqt) && r;

	}		
	
	{
		ECPoint kp;
		UInteger k;
		UInteger_FromUInt(&k, 2);

		EC_MUL(&ec, &k, &p, &kp);

		ECPoint kpt;
		UInteger i7, i12;

		UInteger_FromUInt(&i7, 7);
		UInteger_FromUInt(&i12, 12);
		ECPoint_FromUInt(&kpt, &i7, &i12, 0);

		r = ECPoint_Equal(&kp, &kpt) && r;

	}
	return r;
}



bool SM_Test_ECFp() {
	bool r = true;
	
	r = SM_TEST(Test_SimpleEC()) && r;

	return r;
}
